package com.zcoj.service.impl;

import com.zcoj.core.model.Constants;
import com.zcoj.service.FileService;
import com.zcoj.service.ProblemCaseService;
import com.zcoj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@Service
public class FileServiceImpl implements FileService {


    @Autowired
    private ProblemCaseService problemCaseService;


    @Override
    public File saveFile(MultipartFile file, String abstractFilePathName) {
        return FileUtil.upload(file,abstractFilePathName);
    }

    @Override
    public File uploadFileImg(MultipartFile file, String abstractFilePathName) {
        return FileUtil.uploadImg(file,abstractFilePathName);
    }


    @Override
    public Boolean deleteFile(String abstractFilePathName) {
        return cn.hutool.core.io.FileUtil.del(new File(FileUtil.toDiskPath(abstractFilePathName)));
    }

    @Override
    public File saveProblemTestcase(Integer problemId,MultipartFile file) {
        return FileUtil.uploadProblemTestcase(file, Constants.File.PROBLEM_FILE_FOLDER.getPath()+problemId.toString()+"/", problemCaseService.countByPid(problemId)+1);
    }

    @Override
    public Boolean delAllProblemTestcase(Integer problemId) {
        return cn.hutool.core.io.FileUtil.del(new File(Constants.File.PROBLEM_FILE_FOLDER.getPath()+problemId.toString()));
    }

    @Override
    public Boolean delSingleProblemTestcase(Integer problemId, String testcaseName) {
        return cn.hutool.core.io.FileUtil.del(new File(Constants.File.PROBLEM_FILE_FOLDER.getPath()+problemId.toString()+"/"+testcaseName));
    }

}
